package com.xd100.lzll.gm.util;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.mina.core.buffer.IoBuffer;

public class StringUtil {

	/**
	 * 将String转换为Integer
	 * 
	 * @param s
	 * @return
	 */
	public static Integer parseInt(String s) {
		Integer n = 0;
		try {
			n = Integer.parseInt(s);
		} catch (Exception e) {

		}
		return n;
	}

	/**
	 * 将String转换为Float
	 * 
	 * @param s
	 * @return
	 */
	public static Float parseFloat(String s) {
		Float f = 0.0f;
		try {
			f = Float.parseFloat(s);
		} catch (Exception e) {

		}
		return f;
	}

	/**
	 * 将String转换为Double
	 * 
	 * @param s
	 * @return
	 */
	public static double parseDouble(String s) {
		Double d = 0.0;
		try {
			d = Double.parseDouble(s);
		} catch (Exception e) {

		}
		return d;
	}

	/**
	 * 字符串转换为日期 更多日期处理的请参�?：DateUtil.java
	 * 
	 * @param s
	 * @return
	 */
	public static Date strToDate(String s) {
		Date date = new Date();
		ParsePosition pos = new ParsePosition(0);
		try {
			if (s.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}")) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				date = sdf.parse(s, pos);
			}
		} catch (Exception e) {

		}
		return date;
	}

	public static String join(Object... os) {
		StringBuilder sb = new StringBuilder();
		for (Object o : os) {
			sb.append(o);
		}
		return sb.toString();
	}

	/* 是否是中文符号 */
	// 逗号
	public static boolean isChineseSign(String str) {
		if (str.indexOf("，") != -1) {
			return true;
		}
		return false;
	}

	public static boolean chineseCheck(String str) {
		char[] ch = { '｛', '｝', '（', '）', '：', '—', '，', '；', '。' };
		for (char c : ch) {
			if (str.indexOf(c) != -1) {
				return true;
			}
		}
		return false;
	}

	public static final String getWStr(IoBuffer buf)
			throws UnsupportedEncodingException {
		int MAX_LENGTH = 8 * 1024;
		IoBuffer buff = IoBuffer.allocate(MAX_LENGTH);
		for (int n = 0; n < MAX_LENGTH; n++) {
			byte b1 = buf.get();
			byte b2 = buf.get();
			if (b1 == '\0' && b2 == '\0') {
				break;
			}
			buff.put(b1);
			buff.put(b2);
		}
		// byte[] b = toByteArray(buff);
		byte[] b = buff.array();
		return new String(b, "UTF-16LE");
	}

	/**
	 * @param str
	 * @return 去掉前后空格的字符串
	 */
	public static String trim(String str) {
		if (str == null)
			return "";
		str = str.trim();
		while (str.startsWith(" ")) {
			str = str.substring(1, str.length()).trim();
		}
		return str;
	}

	/**
	 * @param number
	 * @return 保留两位小数
	 */
	public static String double_format(double number) {
		DecimalFormat deci = new DecimalFormat("###.00");
		return deci.format(number);
	}

	public static boolean temp_sql_filter(String sql) {
		boolean flag = true;
		StringTokenizer st = new StringTokenizer(sql.toString(), ",.!() '<>=");
		while (st.hasMoreTokens()) {
			String ss = st.nextToken();
			if (ss.toLowerCase().equals("delete")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("drop")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("update")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("alter")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("truncate")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("create")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("insert")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("load")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("source")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("show")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("mysqldump")) {
				flag = false;
				break;
			} else if (ss.toLowerCase().equals("load")) {
				flag = false;
				break;
			}
		}
		return flag;
	}

	/**
	 *  整型数组转字符串
	 * 
	 * @param arry
	 * @return
	 */
	public static String IntegerArrayToString(Integer[] arry) {
		if (arry == null) {
			return "";
		}
		StringBuilder str = new StringBuilder();
		for (int num : arry) {
			str.append(num);
			str.append(",");
		}
		return str.toString();
	}

	public static String intArrayToString(int[] arry) {
		if (arry == null) {
			return "";
		}
		StringBuilder str = new StringBuilder();
		for (int num : arry) {
			str.append(num);
			str.append(",");
		}
		return str.toString();
	}

	/**
	 *  字符串转整型数组
	 * 
	 * @param str
	 * @param regex
	 *            分隔符
	 * @return
	 */
	public static int[] StringToIntArray(String str, String regex) {
		if (str == null || str.equals("")) {
			return new int[] {};
		}
		int num_arry[];
		try {
			String str_arry[] = str.split(regex);
			num_arry = new int[str_arry.length];
			for (int i = 0; i < str_arry.length; i++) {
				if (str_arry[i].equals(""))
					continue;
				num_arry[i] = Integer.parseInt(str_arry[i]);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new int[] {};
		}
		return num_arry;
	}

	// 判断参数是否为空
	public static boolean paramJudge(String... param) {
		if (param == null) {
			return false;
		}
		for (String par : param) {
			if (par == null || "".equals(par)) {
				return false;
			}
		}
		return true;
	}

	// 把布尔值转换为中文
	public static String booleanToChinese(boolean flag) {
		if (flag) {
			return "成功";
		}
		return "失败";
	}

	// 把布尔值转换为字符串
	public static String booleanToString(boolean flag) {
		if (flag) {
			return "true";
		}
		return "false";
	}

	public static String intToChinese(int num) {
		if (num == 0) {
			return "失败";
		}
		return "成功";
	}

	/**
	 *  计算初始条数
	 * 
	 * @param page
	 *            第几页
	 * @param row
	 *            每页好多行
	 * @return 初始行数
	 */
	public static int startRow(int page, int row) {
		return (page - 1) * row;
	}

	// 根据数量的多少生成相应的问号字符串
	public static String getQuestionMarkString(int num) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < num; i++) {
			if (i < num - 1) {
				sb.append("?,");
			} else {
				sb.append("?");
			}
		}
		return sb.toString();
	}

	// 根据list返回参数列表
	@SuppressWarnings("rawtypes")
	public static String getParmeter(List lst) {
		StringBuffer sb = new StringBuffer();
		for (Object obj : lst) {
			sb.append(obj + ",");
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	/**
	 *  is null ||"" return "0"
	 * 
	 * @param msg
	 * @return
	 */
	public static String isNullString(Object obj) {
		String msg = String.valueOf(obj);
		if (msg == null || "".equals(msg) || "null".equals(msg)) {
			return "0";
		}
		return msg.toString();
	}

	/**
	 * 
	 * 
	 * @param obj
	 * @return
	 */
	public static int objToint(Object obj) {
		return Integer.parseInt(isNullString(obj));
	}

	/**
	 *  deflute 0 array
	 * 
	 * @param length
	 * @return
	 */
	public static Object[] newObjectArray(int length) {
		Object[] obj = new Object[length];
		for (int i = 0; i < length; i++) {
			obj[i] = 0;
		}
		return obj;
	}

	/**
	 * @param sql
	 * @return 判断sql能否对数据库造成改动 正确sql返回true 错误返回false
	 */
	public static boolean isCorrectSql(String sql) {
		sql = sql.toUpperCase();
		List<String> keywordlist = new ArrayList<String>();
		// keywordlist.add("DELETE");
		// keywordlist.add("UPDATE");
		// keywordlist.add("DROP");
		// keywordlist.add("ALTER");
		// keywordlist.add("CREATE");
		// keywordlist.add("TRUNCATE");
		// keywordlist.add("TABLE");
		// keywordlist.add("DATABASE");
		// keywordlist.add("ADD");
		// keywordlist.add("SHOW");
		// keywordlist.add("USE");
		// keywordlist.add("GRANT");
		// keywordlist.add("GRANTS");
		// keywordlist.add("PROCEDURE");
		// keywordlist.add("*");
		for (String keyword : keywordlist) {
			if (sql.indexOf(keyword) != -1) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断是否纯数字组成
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) < '0' || str.charAt(i) > '9') {
				return false;
			}
		}
		return true;
	}

}
